var Select_link = function() {


    var handleSelect = function() {

        var articleType_url = $('.articleType_url').val();
        var articleList_url = $('.articleList_url').val();
        var villageList_url = $('.villageList_url').val();
        var knowledgeType_url = $('.knowledgeType_url').val();
        var knowledgeList_url = $('.knowledgeList_url').val();
        var featurecolumnList_url = $('.featurecolumnList_url').val();
        var featuresList_url = $('.featuresList_url').val();
        var microclassroomType_url = $('.microclassroomType_url').val();
        var productType_url = $('.productType_url').val();
        var productList_url = $('.productList_url').val();

        var defaultContent = '<button type="button" class="btn blue select-btn">选取</button>';

        if(ADMININFO.type != 0) {
            var cityId = ADMININFO.cityId;
            var xianId = ADMININFO.xianId;
            var townId = ADMININFO.townId;
            var villageId = ADMININFO.villageId;

            var defaultData = {
                'cityId' : cityId,
                'xianId' : xianId,
                'townId' : townId,
                'villageId' : villageId,
            };
        }


        // 显示文章分类
        function showArticleType($obj, typeId) {
            var table = $('#articleType-datatable');
            if(table.length <= 0){
                return;
            }
            var params = {
                'typeId' : typeId,
            };
            var data = $.extend(params, defaultData);

            table.dataTable({
                "processing": true,
                "ordering": false, // 禁止排序
                serverSide: true, // 开启服务器模式
                destroy:true, //Cannot reinitialise DataTable,解决重新加载表格内容问题
                "ajax": {
                    url: articleType_url,
                    data: data
                },
                "autoWidth": false,
                "columns": [
                    {"data": "name"},
                    {
                        "width": "10%",
                        "data": "null",
                        "defaultContent": defaultContent
                    }
                ],
                "language": {
                    url: '/public/static/admin/pages/datatable_cn.json'
                },
                "lengthMenu": [[10, 20, 50, 100, 150], [10, 20, 50, 100, 150]],
                "pageLength": 10,
                "order": [
                    [0, "desc"]
                ],
                "columnDefs": [{
                    "orderable": false,
                    "targets": [1]
                }],
                "dom": "<'row'<'.col-md-6 col-sm-12'><'col-md-6 col-sm-12'>r>" +
                "<t>" +
                "<'relative'<'col-md-5'i><'col-md-7'>lp>",
                "fnInitComplete": function() {
                    //var nTrs = table.fnGetNodes();//fnGetNodes获取表格所有行，nTrs[i]表示第i行tr对象
                }
            });

            // 筛选
            table.parents('.modal').on('click', '.doSearch', function(event){
                var param = $(this).parents('.searchForm').serializeObject();

                tableSearch(table, param);
            });

            // 选中
            handleSelected(table, $obj, 2);
        }

        // 显示文章列表
        function showArticleList($obj, typeId) {
            var table = $('#article-datatable');
            if(table.length <= 0){
                return;
            }

            var params = {
                'typeId' : typeId,
            };
            var data = $.extend(params, defaultData);

            //table.dataTable().fnClearTable(); // 将数据清除
            table.dataTable({
                "processing": true,
                serverSide: true, // 开启服务器模式
                destroy:true, //Cannot reinitialise DataTable,解决重新加载表格内容问题
                "ajax": {
                    url: articleList_url,
                    data: data
                },
                "autoWidth": false,
                "columns": [
                    {
                        "data": "iconUrl",
                        "render": function(data, type, row, meta) {
                            return '<a href="'+data+'" target="_blank"><img src="'+data+'" style="width: 40px;height: 40px;"/></a>';
                        }
                    },
                    {"data": "title"},
                    {"data": "createDate"},
                    {
                        "width": "10%",
                        "data": "null",
                        "defaultContent": defaultContent
                    }
                ],
                "language": {
                    url: '/public/static/admin/pages/datatable_cn.json'
                },
                "lengthMenu": [[10, 20, 50, 100, 150], [10, 20, 50, 100, 150]],
                "pageLength": 10,
                "order": [
                    [2, "desc"]
                ],
                "columnDefs": [{
                    "orderable": false,
                    "targets": [0, 3]
                }],
                "dom": "<'row'<'.col-md-6 col-sm-12'><'col-md-6 col-sm-12'>r>" +
                "<t>" +
                "<'relative'<'col-md-5'i><'col-md-7'>lp>",
                "fnInitComplete": function() {
                    //var nTrs = table.fnGetNodes();//fnGetNodes获取表格所有行，nTrs[i]表示第i行tr对象
                }
            });
            //table.DataTable().draw();

            // 筛选
            table.parents('.modal').on('click', '.doSearch', function(event){
                var param = $(this).parents('.searchForm').serializeObject();

                tableSearch(table, param);
            });

            // 选中
            handleSelected(table, $obj, 1);
        }

        // 显示村子列表
        function showVillageList($obj) {
            var table = $('#village-datatable');
            if(table.length <= 0){
                return;
            }

            table.dataTable({
                "processing": true,
                serverSide: true, // 开启服务器模式
                destroy:true, //Cannot reinitialise DataTable,解决重新加载表格内容问题
                "ajax": {
                    url: villageList_url,
                    data: defaultData
                },
                "autoWidth": false,
                "columns": [
                    {
                        "data": "imgUrl",
                        "render": function(data, type, row, meta) {
                            return '<a href="'+data+'" target="_blank"><img src="'+data+'" style="width: 40px;height: 40px;"/></a>';
                        }
                    },
                    {"data": "name"},
                    {
                        "width": "10%",
                        "data": "null",
                        "defaultContent": defaultContent
                    }
                ],
                "language": {
                    url: '/public/static/admin/pages/datatable_cn.json'
                },
                "lengthMenu": [[10, 20, 50, 100, 150], [10, 20, 50, 100, 150]],
                "pageLength": 10,
                "order": [
                    [1, "desc"]
                ],
                "columnDefs": [{
                    "orderable": false,
                    "targets": [0, 2]
                }],
                "dom": "<'row'<'.col-md-6 col-sm-12'><'col-md-6 col-sm-12'>r>" +
                "<t>" +
                "<'relative'<'col-md-5'i><'col-md-7'>lp>",
                "fnInitComplete": function() {
                    //var nTrs = table.fnGetNodes();//fnGetNodes获取表格所有行，nTrs[i]表示第i行tr对象
                }
            });

            // 筛选
            table.parents('.modal').on('click', '.doSearch', function(event){
                var param = $(this).parents('.searchForm').serializeObject();

                tableSearch(table, param);
            });

            // 选中
            handleSelected(table, $obj, 1);
        }

        // 显示农事分类
        function showKnowledgeType($obj) {
            var table = $('#knowledgeType-datatable');
            if(table.length <= 0){
                return;
            }

            table.dataTable({
                "processing": true,
                serverSide: true, // 开启服务器模式
                destroy:true, //Cannot reinitialise DataTable,解决重新加载表格内容问题
                "ajax": {
                    url: knowledgeType_url,
                    data: defaultData
                },
                "autoWidth": false,
                "columns": [
                    {
                        "data": "imgUrl",
                        "render": function(data, type, row, meta) {
                            return '<a href="'+data+'" target="_blank"><img src="'+data+'" style="width: 40px;height: 40px;"/></a>';
                        }
                    },
                    {"data": "name"},
                    {"data": "sorting"},
                    {"data": "createDate"},
                    {
                        "width": "10%",
                        "data": "null",
                        "defaultContent": defaultContent
                    }
                ],
                "language": {
                    url: '/public/static/admin/pages/datatable_cn.json'
                },
                "lengthMenu": [[10, 20, 50, 100, 150], [10, 20, 50, 100, 150]],
                "pageLength": 10,
                "order": [
                    [2, "desc"]
                ],
                "columnDefs": [{
                    "orderable": false,
                    "targets": [0, 1, 4]
                }],
                "dom": "<'row'<'.col-md-6 col-sm-12'><'col-md-6 col-sm-12'>r>" +
                "<t>" +
                "<'relative'<'col-md-5'i><'col-md-7'>lp>",
                "fnInitComplete": function() {
                    //var nTrs = table.fnGetNodes();//fnGetNodes获取表格所有行，nTrs[i]表示第i行tr对象
                }
            });

            // 筛选
            table.parents('.modal').on('click', '.doSearch', function(event){
                var param = $(this).parents('.searchForm').serializeObject();

                tableSearch(table, param);
            });

            // 选中
            handleSelected(table, $obj, 2);
        }

        // 显示农事列表
        function showKnowledgeList($obj) {
            var table = $('#knowledge-datatable');
            if(table.length <= 0){
                return;
            }

            table.dataTable({
                "processing": true,
                serverSide: true, // 开启服务器模式
                destroy:true, //Cannot reinitialise DataTable,解决重新加载表格内容问题
                "ajax": {
                    url: knowledgeList_url,
                    data: defaultData
                },
                "autoWidth": false,
                "columns": [
                    {
                        "data": "imgUrl",
                        "render": function(data, type, row, meta) {
                            return '<a href="'+data+'" target="_blank"><img src="'+data+'" style="width: 40px;height: 40px;"/></a>';
                        }
                    },
                    {"data": "title"},
                    {"data": "sorting"},
                    {"data": "createDate"},
                    {
                        "width": "10%",
                        "data": "null",
                        "defaultContent": defaultContent
                    }
                ],
                "language": {
                    url: '/public/static/admin/pages/datatable_cn.json'
                },
                "lengthMenu": [[10, 20, 50, 100, 150], [10, 20, 50, 100, 150]],
                "pageLength": 10,
                "order": [
                    [2, "desc"]
                ],
                "columnDefs": [{
                    "orderable": false,
                    "targets": [0, 1, 4]
                }],
                "dom": "<'row'<'.col-md-6 col-sm-12'><'col-md-6 col-sm-12'>r>" +
                "<t>" +
                "<'relative'<'col-md-5'i><'col-md-7'>lp>",
                "fnInitComplete": function() {
                    //var nTrs = table.fnGetNodes();//fnGetNodes获取表格所有行，nTrs[i]表示第i行tr对象
                }
            });

            // 筛选
            table.parents('.modal').on('click', '.doSearch', function(event){
                var param = $(this).parents('.searchForm').serializeObject();

                tableSearch(table, param);
            });

            // 选中
            handleSelected(table, $obj, 1);
        }

        // 显示特色专题列表
        function showFeaturecolumnList($obj) {
            var table = $('#featurecolumn-datatable');
            if(table.length <= 0){
                return;
            }

            table.dataTable({
                "processing": true,
                serverSide: true, // 开启服务器模式
                destroy:true, //Cannot reinitialise DataTable,解决重新加载表格内容问题
                "ajax": {
                    url: featurecolumnList_url,
                    data: defaultData
                },
                "autoWidth": false,
                "columns": [
                    {
                        "data": "imgUrl",
                        "render": function(data, type, row, meta) {
                            return '<a href="'+data+'" target="_blank"><img src="'+data+'" style="width: 40px;height: 40px;"/></a>';
                        }
                    },
                    {"data": "name"},
                    {"data": "sorting"},
                    {
                        "width": "10%",
                        "data": "null",
                        "defaultContent": defaultContent
                    }
                ],
                "language": {
                    url: '/public/static/admin/pages/datatable_cn.json'
                },
                "lengthMenu": [[10, 20, 50, 100, 150], [10, 20, 50, 100, 150]],
                "pageLength": 10,
                "order": [
                    [2, "desc"]
                ],
                "columnDefs": [{
                    "orderable": false,
                    "targets": [0, 3]
                }],
                "dom": "<'row'<'.col-md-6 col-sm-12'><'col-md-6 col-sm-12'>r>" +
                "<t>" +
                "<'relative'<'col-md-5'i><'col-md-7'>lp>",
                "fnInitComplete": function() {
                    //var nTrs = table.fnGetNodes();//fnGetNodes获取表格所有行，nTrs[i]表示第i行tr对象
                }
            });

            // 筛选
            table.parents('.modal').on('click', '.doSearch', function(event){
                var param = $(this).parents('.searchForm').serializeObject();

                tableSearch(table, param);
            });

            // 选中
            handleSelected(table, $obj, 2);
        }

        // 显示特色内容列表
        function showFeaturesList($obj) {
            var table = $('#features-datatable');
            if(table.length <= 0){
                return;
            }

            table.dataTable({
                "processing": true,
                serverSide: true, // 开启服务器模式
                destroy:true, //Cannot reinitialise DataTable,解决重新加载表格内容问题
                "ajax": {
                    url: featuresList_url,
                    data: defaultData
                },
                "autoWidth": false,
                "columns": [
                    {
                        "data": "imgUrl",
                        "render": function(data, type, row, meta) {
                            return '<a href="'+data+'" target="_blank"><img src="'+data+'" style="width: 40px;height: 40px;"/></a>';
                        }
                    },
                    {"data": "title"},
                    {"data": "createDate"},
                    {
                        "width": "10%",
                        "data": "null",
                        "defaultContent": defaultContent
                    }
                ],
                "language": {
                    url: '/public/static/admin/pages/datatable_cn.json'
                },
                "lengthMenu": [[10, 20, 50, 100, 150], [10, 20, 50, 100, 150]],
                "pageLength": 10,
                "order": [
                    [2, "desc"]
                ],
                "columnDefs": [{
                    "orderable": false,
                    "targets": [0, 1, 3]
                }],
                "dom": "<'row'<'.col-md-6 col-sm-12'><'col-md-6 col-sm-12'>r>" +
                "<t>" +
                "<'relative'<'col-md-5'i><'col-md-7'>lp>",
                "fnInitComplete": function() {
                    //var nTrs = table.fnGetNodes();//fnGetNodes获取表格所有行，nTrs[i]表示第i行tr对象
                }
            });

            // 筛选
            table.parents('.modal').on('click', '.doSearch', function(event){
                var param = $(this).parents('.searchForm').serializeObject();

                tableSearch(table, param);
            });

            // 选中
            handleSelected(table, $obj, 1);
        }

        // 显示微课堂分类
        function showMicroclassroomType($obj) {
            var table = $('#microclassroomType-datatable');
            if(table.length <= 0){
                return;
            }

            table.dataTable({
                "processing": true,
                "ordering": false, // 禁止排序
                serverSide: true, // 开启服务器模式
                destroy:true, //Cannot reinitialise DataTable,解决重新加载表格内容问题
                "ajax": {
                    url: microclassroomType_url,
                    data: defaultData
                },
                "autoWidth": false,
                "columns": [
                    {"data": "name"},
                    {
                        "width": "10%",
                        "data": "null",
                        "defaultContent": defaultContent
                    }
                ],
                "language": {
                    url: '/public/static/admin/pages/datatable_cn.json'
                },
                "lengthMenu": [[10, 20, 50, 100, 150], [10, 20, 50, 100, 150]],
                "pageLength": 10,
                "dom": "<'row'<'.col-md-6 col-sm-12'><'col-md-6 col-sm-12'>r>" +
                "<t>" +
                "<'relative'<'col-md-5'i><'col-md-7'>lp>",
                "fnInitComplete": function() {
                    //var nTrs = table.fnGetNodes();//fnGetNodes获取表格所有行，nTrs[i]表示第i行tr对象
                }
            });

            // 筛选
            table.parents('.modal').on('click', '.doSearch', function(event){
                var param = $(this).parents('.searchForm').serializeObject();

                tableSearch(table, param);
            });

            // 选中
            handleSelected(table, $obj, 2);
        }

        // 显示产品分类
        function showProductType($obj) {
            var table = $('#productType-datatable');
            if(table.length <= 0){
                return;
            }

            table.dataTable({
                "processing": true,
                "ordering": false, // 禁止排序
                serverSide: true, // 开启服务器模式
                destroy:true, //Cannot reinitialise DataTable,解决重新加载表格内容问题
                "ajax": {
                    url: productType_url,
                    data: defaultData
                },
                "autoWidth": false,
                "columns": [
                    {"data": "name"},
                    {
                        "width": "10%",
                        "data": "null",
                        "defaultContent": defaultContent
                    }
                ],
                "language": {
                    url: '/public/static/admin/pages/datatable_cn.json'
                },
                "lengthMenu": [[10, 20, 50, 100, 150], [10, 20, 50, 100, 150]],
                "pageLength": 10,
                "order": [
                    [0, "desc"]
                ],
                "columnDefs": [{
                    "orderable": false,
                    "targets": [1]
                }],
                "dom": "<'row'<'.col-md-6 col-sm-12'><'col-md-6 col-sm-12'>r>" +
                "<t>" +
                "<'relative'<'col-md-5'i><'col-md-7'>lp>",
                "fnInitComplete": function() {
                    //var nTrs = table.fnGetNodes();//fnGetNodes获取表格所有行，nTrs[i]表示第i行tr对象
                }
            });

            // 筛选
            table.parents('.modal').on('click', '.doSearch', function(event){
                var param = $(this).parents('.searchForm').serializeObject();

                tableSearch(table, param);
            });

            // 选中
            handleSelected(table, $obj, 2);
        }

        // 显示产品列表
        function showProductList($obj) {
            var table = $('#product-datatable');
            if(table.length <= 0){
                return;
            }
            var params = {
                'style' : 1,
            };
            var data = $.extend(params, defaultData);

            table.dataTable({
                "processing": true,
                serverSide: true, // 开启服务器模式
                destroy:true, //Cannot reinitialise DataTable,解决重新加载表格内容问题
                "ajax": {
                    url: productList_url,
                    data: data
                },
                "autoWidth": false,
                "columns": [
                    {
                        "data": "imgUrl",
                        "render": function(data, type, row, meta) {
                            return '<a href="'+data+'" target="_blank"><img src="'+data+'" style="width: 40px;height: 40px;"/></a>';
                        }
                    },
                    {"data": "title"},
                    {"width": "10%", "data": "townName"},
                    {"width": "10%", "data": "villageName"},
                    {
                        "width": "10%",
                        "data": "price",
                        "render": function(data, type, row, meta) {
                            if(!data){
                                return '';
                            }
                            return data / 100 + ' 元 /' + row.unit;
                        }
                    },
                    {"data": "createDate"},
                    {
                        "width": "10%",
                        "data": "null",
                        "defaultContent": defaultContent
                    }
                ],
                "language": {
                    url: '/public/static/admin/pages/datatable_cn.json'
                },
                "lengthMenu": [[10, 20, 50, 100, 150], [10, 20, 50, 100, 150]],
                "pageLength": 10,
                "order": [
                    [5, "desc"]
                ],
                "columnDefs": [{
                    "orderable": false,
                    "targets": [0, 2, 3, 6]
                }],
                "dom": "<'row'<'.col-md-6 col-sm-12'><'col-md-6 col-sm-12'>r>" +
                "<t>" +
                "<'relative'<'col-md-5'i><'col-md-7'>lp>",
                "fnInitComplete": function() {
                    //var nTrs = table.fnGetNodes();//fnGetNodes获取表格所有行，nTrs[i]表示第i行tr对象
                }
            });

            // 筛选
            table.parents('.modal').on('click', '.doSearch', function(event){
                var param = $(this).parents('.searchForm').serializeObject();

                tableSearch(table, param);
            });

            // 选中
            handleSelected(table, $obj, 1);
        }

        function tableSearch(table, params) {

            table.DataTable().search(JSON.stringify(params)).draw();
        }

        var _$obj = '';
        function handleSelected(table, $obj, linkType) {
            _$obj = $obj;
            console.log($obj);
            table.on('click', '.select-btn', function(event) {
                var _this = $(this);
                var dataArr = table.DataTable().rows($(this).parents("tr")).data();
                var id = dataArr[0].id;
                var displayName = dataArr[0].name;

                // 因为没点击一次都会执行一次handleSelected，导致select-btn点击事件会执行多次，所以需清空_$obj，于是第二次以后将不进行赋值
                if(_$obj){
                    /*//$obj.parents('.portlet-sortable').find("input[name='linkId']").val(id);
                    $obj.parents('.portlet-sortable').find("input[name='linkCateId']").val(id);
                    $obj.parents('.portlet-sortable').find("input[name='displayName']").val(displayName);*/
                    if(linkType == 1){
                        _$obj.parent().find("input[name='linkId']").val(id);
                        _$obj.parent().find("input[name='linkCateId']").val('');
                    }else{
                        _$obj.parent().find("input[name='linkId']").val('');
                        _$obj.parent().find("input[name='linkCateId']").val(id);
                    }
                    _$obj.parent().find("input[name='displayName']").val(displayName);
                }
                _$obj = '';

                $(this).parents(".modal").modal('hide');
            });
        }


        var clickSelect = 0;
        $(document).on('blur', '.select-links', function(){
            clickSelect = 0;
        });
        // 选择链接
        //$(document).on('change', '.select-links', function(){
        $(document).on('click', '.select-links', function(){
            if(clickSelect % 2 == 1){
                var _this = $(this);
                var value = $(this).val();

                switch (value) {
                    case '41':
                        // 头条详情--显示文章列表
                        showArticleList(_this, 'toutiao');
                        $('#article-modal').modal('show');
                        break;
                    case '42':
                        // 村子详情--显示村子列表
                        showVillageList(_this);
                        $('#village-modal').modal('show');
                        break;
                    case '43':
                        // 农事列表--显示农事分类
                        showKnowledgeType(_this);
                        $('#knowledgeType-modal').modal('show');
                        break;
                    case '44':
                        // 农事详情--显示农事列表
                        showKnowledgeList(_this);
                        $('#knowledge-modal').modal('show');
                        break;
                    case '45':
                        // 特色专题列表--显示特色专题列表
                        showFeaturecolumnList(_this);
                        $('#featurecolumn-modal').modal('show');
                        break;
                    case '46':
                        // 特色专题详情--显示特色内容列表
                        showFeaturesList(_this);
                        $('#features-modal').modal('show');
                        break;
                    case '47':
                        // 通知详情--显示文章列表
                        showArticleList(_this, 'notice');
                        $('#article-modal').modal('show');
                        break;
                    case '48':
                        // 党建要闻分类
                        showArticleType(_this, 'party');
                        $('#articleType-modal').modal('show');
                        break;
                    case '49':
                        // 党建要闻详情--显示文章列表
                        showArticleList(_this, 'party');
                        $('#article-modal').modal('show');
                        break;
                    case '50':
                        // 微课堂分类
                        showMicroclassroomType(_this);
                        $('#microclassroomType-modal').modal('show');
                        break;
                    case '51':
                        // 产品分类
                        showProductType(_this);
                        $('#productType-modal').modal('show');
                        break;
                    case '52':
                        // 产品详情--显示产品列表
                        showProductList(_this);
                        $('#product-modal').modal('show');
                        break;
                    default: break;
                }
            }

            clickSelect ++;
        });

    };

    return {
        init: function() {
            handleSelect();

        }

    };

}();

jQuery(document).ready(function() {
    Select_link.init();

});